package com.huawei.boss.nio;

import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.http.HttpContentCompressor;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.stream.ChunkedWriteHandler;

/**
 * 后台管理服务
 * 
 * @author c00170351
 */
public class HttpShuntProxyServer {
    private static final int DEFAULT_PORT = 80;
    private int port = DEFAULT_PORT;
    private static final String remoteHost = "www.webxml.com.cn";
    private static final int remotePort = 80;

    public void start() {
        // 配置服务器-使用java线程池作为解释线程
        ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
        // 设置 pipeline factory.
        bootstrap.setPipelineFactory(new ServerPipelineFactory());
        // 绑定端口
        bootstrap.bind(new InetSocketAddress(port));
        System.out.println("admin start on " + port);
    }

    private static class ServerPipelineFactory implements ChannelPipelineFactory {
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();

            //ssl support
            //SSLEngine engine = SSLContext.getInstance("TLS").createSSLEngine();
            //engine.setUseClientMode(false);
            //pipeline.addLast("ssl", new SslHandler(engine));

            pipeline.addLast("decoder", new StringDecoder());
//            pipeline.addLast("encoder", new HttpResponseEncoder());
            // Uncomment the following line if you don't want to handle HttpChunks.
            //pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
            pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
            // Remove the following line if you don't want automatic content compression.
            pipeline.addLast("deflater", new HttpContentCompressor());
            //http处理handler
            
            
            Executor executor = Executors.newCachedThreadPool();
            ClientSocketChannelFactory cf = new NioClientSocketChannelFactory(executor, executor);
            pipeline.addLast("handler", new ShuntProxyHandler(cf, remoteHost, remotePort));
            pipeline.addLast("execution", new ExecutionHandler(executor));
            return pipeline;
        }
    }

    public void setPort(int port) {
        this.port = port;
    }
}